#!/bin/bash

SMX_DIR="/pbfd/m11y/smx_trunk"
[ ! -d "$SMX_DIR" ] && SMX_DIR="/pbfd/manageability/smx_trunk"
[ ! -d "$SMX_DIR" ] && exit 1
TGT_DIR="/var/www/m11ysvr/smx"
SMX_DB="$TGT_DIR/index.db"
SMX_DB_EXP="$TGT_DIR/index_exp.db"
BASE_URL="http://m11ysvr.chn.hp.com/smx"

function rela_dir()
{
  dirname "$1" | awk -F"smx_trunk/" '{print $2}'
}

function rm_space()
{
  echo "$1" | awk '{gsub(" ", "_", $0); print $0}'
}

function gen_sync_mofs()
{
  rm -rf "$TGT_DIR"

  find $SMX_DIR -name "*.mof" | while read moffn
  do
    rdnm="`rela_dir "$moffn"`"
    mfnm="`basename "$moffn"`"
    tgtnm="`rm_space "$mfnm"`"
    [ ! -d "$TGT_DIR/$rdnm" ] && mkdir -p "$TGT_DIR/$rdnm"
    #echo "$TGT_DIR/$rdnm/$tgtnm"
    cp -f "$moffn" "$TGT_DIR/$rdnm/$tgtnm"
  done

  cur_dir=`pwd`
  cp -f $SMX_DIR/common/mofs/cimv2191Final-MOFs.tar $TGT_DIR/common/mofs/
  cd $TGT_DIR/common/mofs/
  tar -xf cimv2191Final-MOFs.tar
  rm -f cimv2191Final-MOFs.tar
  cd $cur_dir
}

function print_url_name()
{
  [ "$1" == "" ] && return 1
  echo $1 | awk '{gsub("^./", "", $0); printf("%s/%s\n", "'"$BASE_URL"'", $0)}'
  return 0
}

function print_mof_inhert(){
  [ "$1" == "" ] && return 1
  MOF="$1"
  cat "$MOF" | grep "^class" | awk -F":" '{\
      gsub(" ", "", $1);\
      gsub("\t", "", $1);\
      gsub("{", "", $1);\
      gsub(" ", "", $2);\
      gsub("\t", "", $2);\
      gsub("{", "", $2);\
      gsub("class", "", $1);\
      printf("%s|%s\n", $1, $2) }'
  return 0
}

function gen_index_db()
{
  cur_dir=`pwd`
  cd $TGT_DIR
  find ./ -name "*.mof" | grep -v "/common/mofs/Experimental/" | while read fn
  do
    print_url_name "$fn"
    print_mof_inhert "$fn"
  done > "$SMX_DB"

  find ./ -name "*.mof" | grep "/common/mofs/Experimental/" | while read fn
  do
    print_url_name "$fn"
    print_mof_inhert "$fn"
  done > "$SMX_DB_EXP"

  cd $cur_dir
}

function print_link(){
  CLS="$1"
  DBF="$2"
  [ "$CLS" == "" ] && return 1
  [ "$DBF" == "" ] && DBF="$SMX_DB"

  cat "$DBF" | awk -F"|" '\
  BEGIN{URL=""}\
  {\
    if (NF==1){\
      URL=$0;\
    } else {\
      if ($1=="'"$CLS"'"){\
        printf("<a href=\"%s\">%s</a>\n", URL, $1);\
        exit 0;\
      }\
    }\
  }\
  END{}'
  return 0
}

function print_parent(){
  CLS="$1"
  DBF="$2"
  [ "$CLS" == "" ] && return 1
  [ "$DBF" == "" ] && DBF="$SMX_DB"

  cat "$DBF" | awk -F"|" '\
  BEGIN{URL=""}\
  {\
    if (NF==1){\
      URL=$0;\
    } else {\
      if ($1=="'"$CLS"'" && $2!="" ){\
        print $2;\
      }\
    }\
  }\
  END{}' | sort
  return 0
}

function print_child(){
  CLS="$1"
  DBF="$2"
  [ "$CLS" == "" ] && return 1
  [ "$DBF" == "" ] && DBF="$SMX_DB"

  cat "$DBF" | awk -F"|" '\
  BEGIN{URL=""}\
  {\
    if (NF==1){\
      URL=$0;\
    } else {\
      if ($2=="'"$CLS"'"){\
        print $1;\
      }\
    }\
  }\
  END{}' | sort
  return 0
}

function print_tree()
{
  [ "$1" == "" ] && return 1
  clsn="$1"
  echo "$2""$clsn"
  print_child "$clsn" | while read sub_clsn
  do
    print_tree "$sub_clsn" "$2+"
  done
  return 0
}

function print_inhert()
{
  [ "$1" == "" ] && return 1
  clsn="$1"
  echo -n "$clsn|"
  print_parent "$clsn" | while read par_clsn
  do
    print_inhert "$par_clsn" "sub"
  done
  [ "$2" != "sub" ] && echo ""
  return 0
}

function gen_top_index(){
  cat "$SMX_DB" | awk -F"|" '(NF>1 && $2==""){print $1}' | sort |\
  while read class_name
  do
    print_tree "$class_name" "+"
  done

}
#gen_sync_mofs
#gen_index_db
#gen_top_index
print_inhert $1
print_link $1 "$SMX_DB"
